package com.zoe.app.selectors;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.zoe.app.config.FilterConfigProperties;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author zbg
 * @date 2023/06/06
 * @description
 */
@Component
@RequiredArgsConstructor
public class FilterSelectorFactory {

    private final FilterConfigProperties filterConfigProperties;
    private final Map<String, IFilterSelector> filterSelectorMap = new ConcurrentHashMap<>();

    public static FilterSelectorFactory of() {
        return SpringUtil.getBean(FilterSelectorFactory.class);
    }

    public IFilterSelector getFilterSelector(String bizCode) {
        IFilterSelector iFilterSelector = filterSelectorMap.get(bizCode);
        if (ObjectUtil.isNotNull(iFilterSelector)) {
            return iFilterSelector;
        }
        List<String> strings = filterConfigProperties.getBizFilters().get(bizCode);
        LocalFilterSelector localFilterSelector = new LocalFilterSelector(strings);
        filterSelectorMap.put(bizCode, localFilterSelector);
        return localFilterSelector;
    }
}
